$(() => {

    const myAjax = (type, url, data, success) => {
        $.ajax({
            type,
            url: 'http://localhost:8080/api/v1' + url,
            headers: {
                Authorization: localStorage.getItem('dsj_token')
            },
            success,
        })
    }

    const getList = () => {
        myAjax('GET', '/admin/category/list', {}, (res) => {
            // console.log('1111', res);
            const htmlStr = template('moban', res)
            // console.log(htmlStr);
            $('#category').html(htmlStr)
            
           
        })
    }
    getList()

    const Date=() => {
        layui.use('laydate', function(){
            var laydate = layui.laydate;
            
            //执行一个laydate实例
            laydate.render({
              elem: '#articleDate' //指定元素
            });
          });
    }
    Date()

    const render=() => {
        tinymce.init({
        selector: '#articleContent',
        language : 'zh_CN'
        });
    }
    render()
       
    let state=''
    $('form').on('submit',(e) => {
        //阻止默认行为
        e.preventDefault()
        // FormData 可以基于 form 快速获取带 name 表单的值
        const fd=new FormData($('form').get(0))
        fd.append('state',state)
        fd.forEach((value,key) => {
            console.log(value,key);

        })
    })

    $.ajax({
        tyoe:'POST',
        url:'http://localhost:8080/api/v1'+'/admin/article/publish',
        data:fd,
        contentType:false,
        processDate:false,
        headers: {
            Authorization: localStorage.getItem('dsj_token')
        },

        success:(res) => {
            if(res.code===200){
                location.href='article_list.html'
            }else{
                alert(res.msg)
            }
        }
    })

    $('.btn-add').on('click',() => {
        state='已发布'
    })
})